<?php
require_once dirname(__FILE__).'\DataBasePDO.php';
class Example {
	function createDatabase()
	{
		$dbArray = Array('host'=>'127.0.0.1', 
								'port'=>'3308', 
								'dbname'=>'MyProject', 
								'user'=>'root', 
								'password'=>'kaida');
	
		$dbScript = "
			/*
			Navicat MySQL Data Transfer
			
			Source Server         : 141_3308
			Source Server Version : 50508
			Source Host           : localhost:3308
			Source Database       : stateanalysis
			
			Target Server Type    : MYSQL
			Target Server Version : 50508
			File Encoding         : 65001
			
			Date: 2013-02-26 15:35:51
			*/
			
			SET FOREIGN_KEY_CHECKS=0;
			
			-- ----------------------------
			-- Table structure for `users`
			-- ----------------------------
			DROP TABLE IF EXISTS `users`;
			CREATE TABLE `users` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `user_id` varchar(30) NOT NULL DEFAULT '',
			  `hashed_password` varchar(40) NOT NULL DEFAULT '',
			  `firstname` varchar(30) DEFAULT '',
			  `lastname` varchar(30) DEFAULT '',
			  `mail` varchar(60) NOT NULL DEFAULT '',
			  `admin` tinyint(1) NOT NULL DEFAULT '0',
			  `status` int(11) NOT NULL DEFAULT '1',
			  `last_login_on` datetime DEFAULT NULL,
			  `language` varchar(5) DEFAULT '',
			  `role_id` int(11) DEFAULT NULL,
			  `created_on` datetime DEFAULT NULL,
			  `updated_on` datetime DEFAULT NULL,
			  `type` varchar(255) DEFAULT NULL,
			  `identity_url` varchar(255) DEFAULT NULL,
			  `mail_notification` varchar(255) NOT NULL DEFAULT '',
			  `user_name` varchar(255) NOT NULL,
			  `offline_usergroup` int(11) NOT NULL DEFAULT '1',
			  `salt` varchar(64) DEFAULT NULL,
			  PRIMARY KEY (`id`),
			  UNIQUE KEY `index_user_on_user_id` (`user_id`) USING BTREE,
			  KEY `index_users_on_id_and_type` (`id`,`type`),
			  KEY `index_users_on_auth_source_id` (`role_id`),
			  KEY `index_users_on_type` (`type`)
			) ENGINE=InnoDB AUTO_INCREMENT=147 DEFAULT CHARSET=utf8;
			
			-- ----------------------------
			-- Records of users
			-- ----------------------------
			INSERT INTO `users` VALUES ('6', 'mgr', '111', '管理员', '博华信智', 'kaida@bhxz.net', '1', '0', null, '', '1339135898', '2012-07-09 10:41:59', null, null, null, '', 'MGR', '2', null);
			INSERT INTO `users` VALUES ('30', 'bpx', 'bpx', '浦祥', '卜', 'bupuxiang@bhxz.net', '1', '0', null, '', '1339135898', '2013-02-18 15:56:34', null, null, null, '', 'bpx', '4', null);
			INSERT INTO `users` VALUES ('139', 'aa_01', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:35:40', null, null, null, '', 'aa_01', '0', null);
			INSERT INTO `users` VALUES ('140', 'aa_02', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:35:45', null, null, null, '', 'aa_02', '0', null);
			INSERT INTO `users` VALUES ('141', 'aa_03', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:35:49', null, null, null, '', 'aa_03', '4', null);
			INSERT INTO `users` VALUES ('142', 'aa_04', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:35:52', null, null, null, '', 'aa_04', '4', null);
			INSERT INTO `users` VALUES ('143', 'aa_05', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:36:02', null, null, null, '', 'aa_05', '4', null);
			INSERT INTO `users` VALUES ('144', 'aa_06', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:36:06', null, null, null, '', 'aa_06', '4', null);
			INSERT INTO `users` VALUES ('145', 'aa_07', 'asdf', 'sadf', 'fasdf', 'asdf@sd.dsa', '1', '0', null, '', '1356498701', '2013-02-22 16:36:13', null, null, null, '', 'aa_07', '4', null);
			INSERT INTO `users` VALUES ('146', 'new_test_01', 'bpxbpx', 'puxiang', 'bu', 'bupuxiang@bhxz.net', '1', '0', null, '', '1347331911', '2013-02-22 17:21:03', null, null, null, '', 'new_test_01', '4', null);
					
		";
	
		if(empty($dbArray['dbname'])) {
			echo "请设置数据库名称！";
			return;
		} else {
			$link = @mysql_connect($dbArray['host'].":".$dbArray['port'], $dbArray['user'], $dbArray['password']);
		
			if(!$link) {
				$errno = @mysql_errno($link);
				$error = @mysql_error($link);
				if($errno == 1045) {
					echo "不能连接数据库，用户名或密码错误！";
					return;
				} elseif($errno == 2003) {
					echo "无法连接数据库！";
					return;
				} else {
					echo "数据库创建失败！";
					return;
				}
			}
		
			mysql_query('set names utf8');

			if(mysql_get_server_info() > '4.1') {
				mysql_query("CREATE DATABASE IF NOT EXISTS `".$dbArray['dbname']."` DEFAULT CHARACTER SET utf8", $link);
			} else {
				mysql_query("CREATE DATABASE IF NOT EXISTS `".$dbArray['dbname']."`", $link);
			}
		
			mysql_select_db($dbArray['dbname'], $link);
		
			$ret = $this->runquery($dbScript,$link);
		
			if($ret && mysql_errno()) {
				echo "数据库创建失败！";
			}elseif($ret) {
				echo "数据库创建成功！";	
			}else{
				echo "请输入SQL脚本！";
			}
		
			mysql_close($link);
		}
		
	}
	function runquery($sql,$dbConn) {
		
		if(!isset($sql) || empty($sql)) return;
		
		$sql = str_replace("\r\n", "\n", $sql);
		
		$ret = array();
		$num = 0;
		foreach(explode(";\n", trim($sql)) as $query) {
			$ret[$num] = '';
			$queries = explode("\n", trim($query));
			foreach($queries as $query) {
				$ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $query[0].$query[1] == '--') ? '' : $query;
			}
			$num++;
		}
		
		unset($sql);
	
		foreach($ret as $query) {
			$query = trim($query);
			if($query) {
				mysql_query($query,$dbConn);
			}
		}
		
		return true;
	}
	
	function createDatabasePDO()
	{
		$dataBase = DataBasePDO::getInstance();
		$db = $dataBase->getConnDB();
		if($db === false)
		{
			return false;
		}
		$sql = "CREATE DATABASE IF NOT EXISTS `MyProject` DEFAULT CHARACTER SET utf8";  
		$stmt = $db->prepare($sql);  
		$bRet = $stmt->execute(array());
		if(!$bRet) 
		{
			return false;
		}
		return $bRet;
	}
	function createTable()
	{
		$dataBase = DataBasePDO::getInstance();
		$db = $dataBase->getConnDB();
		if($db === false)
		{
			return false;
		}
		$sql = "
			DROP TABLE IF EXISTS `users`;
			CREATE TABLE `users` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `user_id` varchar(30) NOT NULL DEFAULT '',
			  `hashed_password` varchar(40) NOT NULL DEFAULT '',
			  `firstname` varchar(30) DEFAULT '',
			  `lastname` varchar(30) DEFAULT '',
			  `mail` varchar(60) NOT NULL DEFAULT '',
			  `admin` tinyint(1) NOT NULL DEFAULT '0',
			  `status` int(11) NOT NULL DEFAULT '1',
			  `last_login_on` datetime DEFAULT NULL,
			  `language` varchar(5) DEFAULT '',
			  `role_id` int(11) DEFAULT NULL,
			  `created_on` datetime DEFAULT NULL,
			  `updated_on` datetime DEFAULT NULL,
			  `type` varchar(255) DEFAULT NULL,
			  `identity_url` varchar(255) DEFAULT NULL,
			  `mail_notification` varchar(255) NOT NULL DEFAULT '',
			  `user_name` varchar(255) NOT NULL,
			  `offline_usergroup` int(11) NOT NULL DEFAULT '1',
			  `salt` varchar(64) DEFAULT NULL,
			  PRIMARY KEY (`id`),
			  UNIQUE KEY `index_user_on_user_id` (`user_id`) USING BTREE,
			  KEY `index_users_on_id_and_type` (`id`,`type`),
			  KEY `index_users_on_auth_source_id` (`role_id`),
			  KEY `index_users_on_type` (`type`)
			) ENGINE=InnoDB AUTO_INCREMENT=147 DEFAULT CHARSET=utf8;
		";  
		$stmt = $db->prepare($sql);  
		$bRet = $stmt->execute(array());
		if(!$bRet) 
		{
			return false;
		}
		return $bRet;
	}
}

?>